package com.kinghood.productcenter.controller.product;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.model.KingHoodException;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.constant.redis.RedisConstant;
import com.kinghood.infrastructure.model.dto.StatusRemarkReqDTO;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.productcenter.common.model.dto.base.ListProductQueryDTO;
import com.kinghood.productcenter.common.model.dto.product.*;
import com.kinghood.productcenter.common.model.vo.product.ProductDetailVO;
import com.kinghood.productcenter.manager.IProductBaseManager;
import com.kinghood.productcenter.service.product.IProductBaseService;
import com.kinghood.productcenter.service.product.handler.ProductBaseHandler;
import com.kinghood.redis.aspect.RedisLock;
import com.kinghood.redis.util.RedissonManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 商品款式基础表 前端控制器
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/productBase")
public class ProductBaseController {

    @Resource
    private IProductBaseManager productBaseManager;
    private final IProductBaseService productBaseService;
    private final RedissonManager redissonManager;

    /**
     * 商品列表
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/25
     */
    @PostMapping("/listProduct")
    public KingHoodResponseVO listProduct(@Valid @RequestBody ListProductQueryDTO param) {
        return KingHoodResponseVO.success(productBaseManager.listProduct(param));
    }


    /**
     * 导出产品
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/25
     */
    @PostMapping("/exportProduct")
    public void exportProduct(@Valid @RequestBody ListProductQueryDTO param, HttpServletResponse response) throws IOException {
        productBaseManager.exportProduct(param, response);
    }

    /**
     * 保存商品
     *
     * @param in 入参
     * @return 商品id
     * @author zfj
     * @date 2023/12/25
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-保存商品")
    @PostMapping("/addOrUpdateProduct")
    public KingHoodResponseVO<Long> addOrUpdateProduct(@Valid @RequestBody ProductReqDTO in) {

        ProductBaseHandler.handleSaveProduct(in.getSkuList(),in.getSkuPriceList());
        if(Objects.nonNull(in.getProductBase().getId())){
            return KingHoodResponseVO.success(productBaseService.updateProduct(in));
        }

        return KingHoodResponseVO.success(productBaseService.addProduct(in));
    }


    /**
     * 保存商品
     *
     * @param in 入参
     * @return 商品id
     * @author zfj
     * @date 2023/12/25
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-保存商品")
    @PostMapping("/addProduct")
    public KingHoodResponseVO<Long> addProduct(@Valid @RequestBody ProductReqDTO in) {

        ProductBaseHandler.handleSaveProduct(in.getSkuList(),in.getSkuPriceList());
        return KingHoodResponseVO.success(productBaseService.addProduct(in));
    }

    /**
     * 修改商品
     *
     * @param in 入参
     * @return 商品id
     * @author zfj
     * @date 2023/12/25
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-修改商品")
    @PostMapping("/updateProduct")
    public KingHoodResponseVO<Long> updateProduct(@RequestBody ProductReqDTO in) {

        ProductBaseHandler.handleSaveProduct(in.getSkuList(),in.getSkuPriceList());
        return KingHoodResponseVO.success(productBaseService.updateProduct(in));
    }

    /**
     * 商品详情
     *
     * @param in 入参
     * @return 商品信息
     * @author zfj
     * @date 2023/12/25
     */
    @PostMapping("/getProduct")
    public KingHoodResponseVO<ProductDetailVO> getProduct(@RequestBody GetProductReqDTO in) {

        return KingHoodResponseVO.success(productBaseManager.getProduct(in));
    }

    /**
     * excel导入
     *
     * @param file     文件
     * @param response 响应
     * @author zfj
     * @date 2023/12/25
     */
    @RedisLock(key = RedisConstant.LOCK_UPLOAD_PRODUCT)
    @PostMapping("/uploadProduct")
    public void uploadProduct(MultipartFile file, HttpServletResponse response) {

        log.info("uploadProduct:===》导入商品");
        productBaseManager.uploadProduct(file, response);
    }

    /**
     * 导入数据回显
     *
     * @param file     文件
     * @return 数据
     * @author zfj
     * @date 2023/12/25
     */
    @PostMapping("/importData")
    public KingHoodResponseVO<List<Map<String, String>>> importData(MultipartFile file) {

        return KingHoodResponseVO.success(productBaseManager.importData(file));
    }

    /**
     * 上传产品图片
     *
     * @param file     文件
     * @param response 回答
     * @author jp
     * @date 2023/12/28
     */
    @PostMapping("/uploadProductPicture")
    public Object uploadProductPicture(MultipartFile file, @RequestParam(value = "type") String type, HttpServletResponse response) {
        if (file.isEmpty()) {
            throw new KingHoodException(-1, "文件不能为空");
        }
        log.info("uploadProduct:===》导入商品图片");
        productBaseManager.uploadProductPicture(file, type, response);
        return KingHoodResponseVO.success();
    }

    /**
     * 导出图片
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/25
     */
    @PostMapping("/exportProductPicture")
    public KingHoodResponseVO exportProductPicture(@Valid @RequestBody ListProductQueryDTO param) {
        if (StringUtils.isBlank(param.getPictureType())) {
            throw new KingHoodException(-1, "图片类型不能为空");
        }
        return productBaseManager.exportProductPicture(param);
    }

    /**
     * 新增时的配置信息
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/28
     */
    @PostMapping("/getConfigInfo")
    public KingHoodResponseVO getConfigInfo(@RequestBody ProductAddTypeDTO param) {
        return KingHoodResponseVO.success(productBaseService.getConfigInfo(param));
    }

    /**
     * 删除产品
     *
     * @param id id
     * @author jp
     * @date 2023/12/29
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-删除商品")
    @RequestMapping(value = "/delProduct/{id}")
    public KingHoodResponseVO delProduct(@PathVariable("id") Long id) {
        productBaseService.delProduct(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 更新产品状态
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/29
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-修改状态")
    @PostMapping(value = "/updateProductStatus")
    public KingHoodResponseVO updateProductStatus(@RequestBody StatusRemarkReqDTO param) {
        if (CollectionUtils.isEmpty(param.getIdList())) {
            throw new KingHoodException(-1, "id不能为空");
        }
        if (Objects.isNull(param.getStatus())) {
            throw new KingHoodException(-1, "状态不能为空");
        }
        productBaseService.updateProductStatus(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 新增标签
     *
     * @param param 参数
     * @author jp
     * @date 2024/01/03
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-新增商品标签")
    @PostMapping(value = "/addLabel")
    public KingHoodResponseVO addLabel(@Valid @RequestBody ProductLabelReqDTO param) {
        productBaseService.addLabel(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 获取产品标签
     *
     * @param param 参数
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/11 17:26
     */
    @PostMapping(value = "/getProductLabel")
    public KingHoodResponseVO getProductLabel(@Valid @RequestBody ProductLabelListReqDTO param) {
        return KingHoodResponseVO.success(productBaseService.getProductLabel(param));
    }

    /**
     * del产品标签
     *
     * @param id 身份证件
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/11 17:49
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_PRODUCT,content = "商品管理-删除商品标签")
    @RequestMapping(value = "/delProductLabel/{id}")
    public KingHoodResponseVO delProductLabel(@PathVariable("id") Long id) {
        productBaseService.delProductLabel(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 更新产品标签时间
     *
     * @param param 参数
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/12 14:59
     */
    @RequestMapping(value = "/updateProductLabelTime")
    public KingHoodResponseVO updateProductLabelTime(@RequestBody ProductLabelReqDTO param) {
        productBaseService.updateProductLabelTime(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 获取产品属性
     *
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/15 15:53
     */
    @RequestMapping(value = "/getProductFields")
    public KingHoodResponseVO getProductFields(@RequestParam("businessCode") String businessCode) {
        return KingHoodResponseVO.success(productBaseService.getProductFields(businessCode));
    }

    /**
     * 获取产品搜索属性
     *
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/15 15:53
     */
    @RequestMapping(value = "/getProductSearchFields")
    public KingHoodResponseVO getProductSearchFields(@RequestParam("businessCode") String businessCode) {
        return KingHoodResponseVO.success(productBaseService.getProductSearchFields(businessCode));
    }
}
